package es.cazadoresmadridejos.beans.manager.session;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import es.cazadoresmadridejos.beans.manager.reason.ReasonManager;
import es.cazadoresmadridejos.model.Partner;
import es.cazadoresmadridejos.model.Reason;
import es.cazadoresmadridejos.objs.security.User;
import es.cazadoresmadridejos.util.model.ReasonUtil;

public class UserPreferences implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 628273661904622697L;
	
	@Autowired private ReasonManager reasonManager;
	
	public void init(){}
	
	public Reason getReason(){
		Reason reason;
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	
		if(user.getReason() == null){
			reason = reasonManager.getByReason(Long.valueOf(ReasonUtil.getCurrentReason()));
			if(reason == null){
				reason = new Reason();
				reason.setYear(ReasonUtil.getCurrentReason());
				reasonManager.save(reason);
			}
		}else{
			reason = user.getReason();
		}
		
		return reason;
	}
	public Partner getPartner(){
		return ((User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPartner();
	}
}
